#!/bin/bash

#
# Installation of JetBrains Intellij IDEA.
# 
# Target platform: Based on Ubuntu(Ubuntu Kylin/Ubuntu Desktop/Linux Mint/ZorinOS/DeepinOS), include graphical interface, and not applicable to any server version.
# Create date: 2019/07/08
# Create user: SteveJrong
# File version: 1.0.0

# Import some shells.
. common/common.sh

# ######################### start of variables #########################
APP_PACKAGE_KEYWORD_1="idealU"
APP_PACKAGE_KEYWORD_2="2019.1.3"
APP_PACKAGE_SUFFIX="tar.gz"
APP_PACKAGE_NAME=$APP_PACKAGE_KEYWORD_1-$APP_PACKAGE_KEYWORD_2-linux-x64
INSTALL_DIRECTORY_NAME=$APP_PACKAGE_KEYWORD_1-$APP_PACKAGE_KEYWORD_2
APP_PACKAGE_DOWNLOAD_LINK="https://download.jetbrains.8686c.com/idea/ideaIU-2019.1.3.tar.gz"
# ######################### end of variables #########################

# ######################### start of functions #########################
# ######################### end of functions #########################

# ######################### start of install JetBrains Intellij IDEA #########################
echo '------------------------- Starting install JetBrains Intellij IDEA.'

echo '------------------------- Installation status checked.'
if [ -e "$USER_PATH/$INSTALL_DIRECTORY_NAME" ]; then
    # Installation directory already exists, need delete first.
    echo '------------------------- Installation directory already exists, need delete first.'
	sudo rm -rf $USER_PATH/$INSTALL_DIRECTORY_NAME
fi
sudo mkdir $USER_PATH/$INSTALL_DIRECTORY_NAME
echo -e "------------------------- Done.\n"
sleep 5

echo '------------------------- Checking temporary file path.'
if [ ! -d "$TEMP_FILE_PATH" ]; then
	# Temporary directory does not exist, need to create it.
	echo '------------------------- Temporary directory does not exist, create it first.'

	sudo mkdir -p $TEMP_FILE_PATH
	sudo chmod -R 777 $TEMP_FILE_PATH
else
	# Temporary directory already exists, need to check of old temporary files.
	checkOldTemporaryFiles=`find $TEMP_FILE_PATH -iname "*$APP_PACKAGE_NAME*.$APP_PACKAGE_SUFFIX"`
	if [ -f "$checkOldTemporaryFiles" ]; then
		echo '------------------------- Compression package file already exists, delete it first.'

		# Application package file already exists, need delete first.
		sudo rm -f $checkOldTemporaryFiles
	fi
fi
echo -e "------------------------- Done.\n"
sleep 5

echo '------------------------- Downloading compression package.'
wget -P $TEMP_FILE_PATH $APP_PACKAGE_DOWNLOAD_LINK -O $TEMP_FILE_PATH/$APP_PACKAGE_NAME.$APP_PACKAGE_SUFFIX
echo -e "------------------------- Done.\n"
sleep 5

echo '------------------------- Decompressing compression package.'
sudo tar -zxvf $TEMP_FILE_PATH/$APP_PACKAGE_NAME.$APP_PACKAGE_SUFFIX -C $USER_PATH/$INSTALL_DIRECTORY_NAME
echo -e "------------------------- Done.\n"
sleep 5

echo '------------------------- Setting installation directory permissions.'
sudo chmod 777 $USER_PATH/$INSTALL_DIRECTORY_NAME
echo -e "------------------------- Done.\n"

outputSuccessMsg "Install JetBrains Intellij IDEA is complete!"
outputSuccessMsg "You can enter the '$USER_PATH/$INSTALL_DIRECTORY_NAME/Xxx/bin', and start with command './idea.sh'!"
# ######################### end of install JetBrains Intellij IDEA #########################